package compiler;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PushbackReader;
import java.io.Reader;
import java.util.ArrayList;
import java.util.HashSet;

import jxl.read.biff.BiffException;

import compiler.Exceptions.SyntaxErrorException;
import compiler.Exceptions.UnexpectedEOFException;

public class Lexico
{
	@SuppressWarnings("static-access")
	public static void main(String args[]) throws SyntaxErrorException, UnexpectedEOFException
	{
		Parser p = new Parser(false);
		String f = "Matriz transicion estados.xls";
		Excel ex = new Excel();
		
		p.reader = abrir(args[0]);
		try {
			p.proceso = ex.excelProceso(f);
			p.nuevo_estado = ex.excelNuevoEstado(f);
			
		} catch (BiffException | IOException e1) {
			p.yyerror(e1.getMessage());
		}

		p.yyparse();
		
		test();
		Metodos.getTabla_simbolos().mostrar();
		cerrar(p.reader);
	}

	public static PushbackReader abrir(String string)
	{
		File f = null;
		InputStream i = null;
	
		f = new File(string);
		try 
		{
			i = new FileInputStream(f);
		} 
		catch (FileNotFoundException e) 
		{
			System.out.println(e.toString());
		}
		Reader r = new InputStreamReader(i);
		return new PushbackReader(r);
	}
	
	public static void test()
	{
		ArrayList<Token> tabla = Tabla.simbolos;
		HashSet<Token> set = new HashSet<Token>();
		
		// los Sets por definicion no agregan elementos repetidos
		for(Token t : tabla)
			set.add(t);
		
		if(set.size() < tabla.size())
			System.out.println("\n\n\n ERROR: Elementos repetidos en la tabla de simbolos.");
	}
	
	public static void cerrar(Reader r)
	{
		try
		{
			r.close();
		}
		
		catch (IOException e)
		{
			e.printStackTrace();
		}
	}
}